﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Catering_Rules
{
    public class AreaRules
    {
        private readonly Catering_Operation.AreaRules dal = new Catering_Operation.AreaRules();
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Catering_Model.AreaRules model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.AreaRules model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(int AreaID)
        {
            return dal.DeleteList(AreaID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.AreaRules GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Catering_Model.AreaRules GetModelByCache(int id)
        {

            string CacheKey = "AreaRulesModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Catering_Model.AreaRules)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.AreaRules> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.AreaRules> DataTableToList(DataTable dt)
        {
            List<Catering_Model.AreaRules> modelList = new List<Catering_Model.AreaRules>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Catering_Model.AreaRules model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Catering_Model.AreaRules();
                    if (dt.Rows[n]["ARID"].ToString() != "")
                    {
                        model.ARID = int.Parse(dt.Rows[n]["ARID"].ToString());
                    }
                    model.RawIDs = dt.Rows[n]["RawIDs"].ToString();
                    model.AreaID = int.Parse(dt.Rows[n]["AreaID"].ToString());
                    model.ARName = dt.Rows[n]["ARName"].ToString();
                    modelList.Add(model);
                }
                dt.Dispose();
            }
            return modelList;
        }

        public string GetARIDsByAreaID(int AreaID)
        {
            DataTable dt = dal.GetList(" AreaID=" + AreaID + " ").Tables[0];
            string result = "";
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int n = 0; n < dt.Rows.Count; n++)
                {
                    if (dt.Rows[n]["ARID"].ToString() != "")
                    {
                        result += dt.Rows[n]["ARID"].ToString() + ',';
                    }
                }
                dt.Dispose();
            }
            return result.TrimEnd(',');
        }

        /// <summary>
        /// 获取原料ID
        /// </summary>
        /// <returns></returns>
        public string GetAreaRulesRawIDsByID()
        {
            DataTable dt = GetAllList().Tables[0];
            string result = "";
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int n = 0; n < dt.Rows.Count; n++)
                {
                    if (dt.Rows[n]["RawIDs"].ToString() != "")
                    {
                        result += dt.Rows[n]["RawIDs"].ToString() + ',';
                    }
                }
                dt.Dispose();
            }
            return result.TrimEnd(',');
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        #endregion  Method
    }
}
